<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 2.5.&nbsp; Limits</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch02lev1sec4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch02lev1sec6.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="ch02lev1sec5"></a>
<h3 class="docSection1Title">2.5. Limits</h3>
<p class="docText">The implementations define many magic numbers and constants. Many of these have been hard coded into programs or were determined using ad hoc techniques. With the various standardization efforts that we've described, more portable methods are now provided to determine these magic numbers and implementation-defined limits, greatly aiding the portability of our software.</P>
<p class="docText">Two types of limits are needed:</P>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">Compile-time limits (e.g., what's the largest value of a short integer?)</P></div></LI><LI><div style="font-weight:normal"><p class="docList">Runtime limits (e.g., how many characters in a filename?)</p></div></LI></ol></div>
<p class="docText">Compile-time limits can be defined in headers that any program can include at compile time. But runtime limits require the process to call a function to obtain the value of the limit.</P>
<p class="docText"><a name="idd1e10095"></a><a name="idd1e10100"></a><a name="idd1e10105"></a>Additionally, some limits can be fixed on a given implementationand could therefore be defined statically in a headeryet vary on another implementation and would require a runtime function call. An example of this type of limit is the maximum number of characters in a filename. Before SVR4, System V historically allowed only 14 characters in a filename, whereas BSD-derived systems increased this number to 255. Most UNIX System implementations these days support multiple file system types, and each type has its own limit. This is the case of a runtime limit that depends on where in the file system the file in question is located. A filename in the root file system, for example, could have a 14-character limit, whereas a filename in another file system could have a 255-character limit.</P>
<p class="docText">To solve these problems, three types of limits are provided:</p>
<div style="font-weight:bold"><ol class="docList" type="1"><LI><div style="font-weight:normal"><p class="docList">Compile-time limits (headers)</p></div></LI><LI><div style="font-weight:normal"><p class="docList">Runtime limits that are not associated with a file or directory (the <tt>sysconf</tt> function)</P></div></li><LI><div style="font-weight:normal"><p class="docList">Runtime limits that are associated with a file or a directory (the <tt>pathconf</tt> and <tt>fpathconf</tt> functions)</P></div></li></ol></div>
<p class="docText">To further confuse things, if a particular runtime limit does not vary on a given system, it can be defined statically in a header. If it is not defined in a header, however, the application must call one of the three <tt>conf</tt> functions (which we describe shortly) to determine its value at runtime.</P>
<a name="ch02lev2sec12"></a>
<H4 class="docSection2Title">2.5.1. ISO C Limits</h4>
<p class="docText"><a name="idd1e10156"></a><a name="idd1e10161"></a><a name="idd1e10166"></a><a name="idd1e10171"></a><a name="idd1e10176"></a><a name="idd1e10181"></a><a name="idd1e10186"></a><a name="idd1e10189"></a><a name="idd1e10194"></a><a name="idd1e10199"></a><a name="idd1e10204"></a><a name="idd1e10209"></a><a name="idd1e10214"></a><a name="idd1e10217"></a><a name="idd1e10222"></a><a name="idd1e10227"></a><a name="idd1e10232"></a><a name="idd1e10237"></a><a name="idd1e10240"></a><a name="idd1e10245"></a><a name="idd1e10250"></a><a name="idd1e10255"></a><a name="idd1e10260"></a><a name="idd1e10265"></a><a name="idd1e10268"></a><a name="idd1e10273"></a><a name="idd1e10278"></a><a name="idd1e10283"></a><a name="idd1e10288"></a><a name="idd1e10293"></a><a name="idd1e10298"></a><a name="idd1e10303"></a><a name="idd1e10308"></a>All the limits defined by ISO C are compile-time limits. <a class="docLink" href="#ch02fig06">Figure 2.6</a> shows the limits from the C standard that are defined in the file <tt>&lt;limits.h&gt;</tt>. These constants are always defined in the header and don't change in a given system. The third column shows the minimum acceptable values from the ISO C standard. This allows for a system with 16-bit integers using one's-complement arithmetic. The fourth column shows the values from a Linux system with 32-bit integers using two's-complement arithmetic. Note that none of the unsigned data types has a minimum value, as this value must be 0 for an unsigned data type. On a 64-bit system, the values for <tt>long</tt> integer maximums match the maximum values for <tt>long long</tt> integers.</p>
<a name="ch02fig06"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><h5 class="docTableTitle">Figure 2.6. Sizes of integral values from <tt>&lt;limits.h&gt;</tt></H5></caption><colgroup><col width="75"><col width="175"><col width="125"><col width="125"></colgroup><thead><tr><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Name</span></P></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Description</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Minimum acceptable value</span></P></th><th class="bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Typical value</span></p></th></tr></thead><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>CHAR_BIT</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">bits in a <tt>char</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">8</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">8</p></td></tr><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>CHAR_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">max value of <tt>char</tt></P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">(see later)</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">127</p></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>CHAR_MIN</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">min value of <tt>char</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">(see later)</P></td><td class="docTableCell" align="right" valign="top"><p class="docText">128</p></td></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>SCHAR_MAX</tt></p></TD><td class="rightBorder" align="left" valign="top"><p class="docText">max value of <tt>signed char</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">127</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">127</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>SCHAR_MIN</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">min value of <tt>signed char</tt></p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">127</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">128</P></TD></tr><TR><TD class="rightBorder bottomBorder" align="left" valign="top"><p class="docText"><tt>UCHAR_MAX</tt></P></td><TD class="rightBorder bottomBorder" align="left" valign="top"><p class="docText">max value of <tt>unsigned char</tt></p></TD><TD class="rightBorder bottomBorder" align="right" valign="top"><p class="docText">255</P></td><TD class="bottomBorder" align="right" valign="top"><p class="docText">255</P></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>INT_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">max value of <tt>int</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">2,147,483,647</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>INT_MIN</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">min value of <tt>int</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">2,147,483,648</p></td></TR><TR><td class="rightBorder bottomBorder" align="left" valign="top"><p class="docText"><tt>UINT_MAX</tt></P></TD><TD class="rightBorder bottomBorder" align="left" valign="top"><p class="docText">max value of <tt>unsigned int</tt></p></TD><TD class="rightBorder bottomBorder" align="right" valign="top"><p class="docText">65,535</P></td><TD class="bottomBorder" align="right" valign="top"><p class="docText">4,294,967,295</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>SHRT_MIN</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">min value of <tt>short</tt></P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">32,768</P></td></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>SHRT_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">max value of <tt>short</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">32,767</p></td></tr><tr><td class="rightBorder bottomBorder" align="left" valign="top"><p class="docText"><tt>USHRT_MAX</tt></p></td><td class="rightBorder bottomBorder" align="left" valign="top"><p class="docText">max value of <tt>unsigned short</tt></P></TD><td class="rightBorder bottomBorder" align="right" valign="top"><p class="docText">65,535</P></TD><TD class="bottomBorder" align="right" valign="top"><p class="docText">65,535</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>LONG_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">max value of <tt>long</tt></P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">2,147,483,647</P></TD><td class="docTableCell" align="right" valign="top"><p class="docText">2,147,483,647</P></TD></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>LONG_MIN</tt></p></TD><td class="rightBorder" align="left" valign="top"><p class="docText">min value of <tt>long</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">2,147,483,647</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">2,147,483,648</p></td></tr><tr><td class="rightBorder bottomBorder" align="left" valign="top"><p class="docText"><tt>ULONG_MAX</tt></p></td><td class="rightBorder bottomBorder" align="left" valign="top"><p class="docText">max value of <tt>unsigned long</tt></p></td><td class="rightBorder bottomBorder" align="right" valign="top"><p class="docText">4,294,967,295</p></td><td class="bottomBorder" align="right" valign="top"><p class="docText">4,294,967,295</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>LLONG_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">max value of <tt>long long</tt></P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">9,223,372,036,854,775,807</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">9,223,372,036,854,775,807</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>LLONG_MIN</tt></p></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">min value of <tt>long long</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">9,223,372,036,854,775,807</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">9,223,372,036,854,775,808</P></td></TR><tr><td class="rightBorder bottomBorder" align="left" valign="top"><p class="docText"><tt>ULLONG_MAX</tt></p></td><td class="rightBorder bottomBorder" align="left" valign="top"><p class="docText">max value of <tt>unsigned long long</tt></p></td><td class="rightBorder bottomBorder" align="right" valign="top"><p class="docText">18,446,744,073,709,551,615</p></td><td class="bottomBorder" align="right" valign="top"><p class="docText">18,446,744,073,709,551,615</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>MB_LEN_MAX</tt></p></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">max number of bytes in a multibyte character constant</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">1</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">16</P></TD></tr></table></P><br>
<p class="docText">One difference that we will encounter is whether a system provides signed or unsigned character values. From the fourth column in <a class="docLink" href="#ch02fig06">Figure 2.6</a>, we see that this particular system uses signed characters. We see that <tt>CHAR_MIN</tt> equals <tt>SCHAR_MIN</tt> and that <tt>CHAR_MAX</tt> equals <tt>SCHAR_MAX</tt>. If the system uses unsigned characters, we would have <tt>CHAR_MIN</tt> equal to 0 and <tt>CHAR_MAX</tt> equal to <tt>UCHAR_MAX</tt>.</P>
<p class="docText">The floating-point data types in the header <tt>&lt;float.h&gt;</tt> have a similar set of definitions. Anyone doing serious floating-point work should examine this file.</P>
<p class="docText">Another ISO C constant that we'll encounter is <tt>FOPEN_MAX</tt>, the minimum number of standard I/O streams that the implementation guarantees can be open at once. This value is in the <tt>&lt;stdio.h&gt;</tt> header, and its minimum value is 8. The POSIX.1 value <tt>STREAM_MAX</tt>, if defined, must have the same value as <tt>FOPEN_MAX</tt>.</P>
<p class="docText">ISO C also defines the constant <tt>TMP_MAX</tt> in <tt>&lt;stdio.h&gt;</tt>. It is the maximum number of unique filenames generated by the <tt>tmpnam</tt> function. We'll have more to say about this constant in <a class="docLink" href="ch05lev1sec13.html#ch05lev1sec13">Section 5.13</a>.</p>
<p class="docText">In <a class="docLink" href="#ch02fig07">Figure 2.7</a>, we show the values of <tt>FOPEN_MAX</tt> and <tt>TMP_MAX</tt> on the four platforms we discuss in this book.</P>
<a name="ch02fig07"></a><P><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><h5 class="docTableTitle">Figure 2.7. ISO limits on various platforms</H5></caption><colgroup><col width="100"><col width="75"><col width="75"><col width="80"><col width="50"></colgroup><thead><TR><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle"><p class="docText"><span class="docEmphRoman">Limit</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">FreeBSD 5.2.1</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Linux 2.4.22</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Mac OS X 10.3</span></p></th><th class="bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Solaris 9</span></P></th></tr></thead><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>FOPEN_MAX</tt></P></td><td class="rightBorder" align="right" valign="top"><p class="docText">20</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">16</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">20</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">20</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>TMP_MAX</tt></p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">308,915,776</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">238,328</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">308,915,776</P></TD><td class="docTableCell" align="right" valign="top"><p class="docText">17,576</P></td></TR></table></P><BR>
<blockquote>
<p class="docText">ISO C also defines the constant <tt>FILENAME_MAX</tt>, but we avoid using it, because some operating system implementations historically have defined it to be too small to be of use.</p>
</blockquote>

<a name="ch02lev2sec13"></a>
<H4 class="docSection2Title">2.5.2. POSIX Limits</H4>
<p class="docText">POSIX.1 defines numerous constants that deal with implementation limits of the operating system. Unfortunately, this is one of the more confusing aspects of POSIX.1. Although POSIX.1 defines numerous limits and constants, we'll only concern ourselves with the ones that affect the base POSIX.1 interfaces. These limits and constants are divided into the following five categories:</p>
<div style="font-weight:bold"><ol class="docList" type="1"><LI><div style="font-weight:normal"><p class="docList"><a name="idd1e10989"></a><a name="idd1e10994"></a><a name="idd1e10997"></a><a name="idd1e11002"></a><a name="idd1e11007"></a><a name="idd1e11012"></a><a name="idd1e11017"></a><a name="idd1e11022"></a><a name="idd1e11027"></a><a name="idd1e11032"></a><a name="idd1e11037"></a><a name="idd1e11042"></a><a name="idd1e11047"></a><a name="idd1e11052"></a><a name="idd1e11057"></a><a name="idd1e11062"></a><a name="idd1e11067"></a><a name="idd1e11072"></a><a name="idd1e11077"></a><a name="idd1e11082"></a><a name="idd1e11087"></a><a name="idd1e11092"></a><a name="idd1e11097"></a><a name="idd1e11102"></a><a name="idd1e11107"></a><a name="idd1e11112"></a><a name="idd1e11117"></a><a name="idd1e11122"></a><a name="idd1e11127"></a><a name="idd1e11132"></a><a name="idd1e11137"></a><a name="idd1e11142"></a><a name="idd1e11147"></a><a name="idd1e11152"></a><a name="idd1e11157"></a><a name="idd1e11162"></a><a name="idd1e11167"></a><a name="idd1e11172"></a><a name="idd1e11177"></a><a name="idd1e11182"></a><a name="idd1e11187"></a><a name="idd1e11192"></a><a name="idd1e11197"></a><a name="idd1e11202"></a><a name="idd1e11207"></a><a name="idd1e11212"></a><a name="idd1e11217"></a><a name="idd1e11222"></a><a name="idd1e11227"></a><a name="idd1e11230"></a><a name="idd1e11235"></a><a name="idd1e11240"></a><a name="idd1e11245"></a><a name="idd1e11250"></a><a name="idd1e11255"></a>Invariant minimum values: the 19 constants in <a class="docLink" href="#ch02fig08">Figure 2.8</a></P></div></li><li><div style="font-weight:normal"><p class="docList">Invariant value: <tt>SSIZE_MAX</tt></p></div></li><LI><div style="font-weight:normal"><p class="docList">Runtime increasable values: <tt>CHARCLASS_NAME_MAX</tt>, <tt>COLL_WEIGHTS_MAX</tt>, <tt>LINE_MAX</tt>, <tt>NGROUPS_MAX</tt>, and <tt>RE_DUP_MAX</tt></p></div></LI><li><div style="font-weight:normal"><p class="docList">Runtime invariant values, possibly indeterminate: <tt>ARG_MAX</tt>, <tt>CHILD_MAX</tt>, <tt>HOST_NAME_MAX</tt>, <tt>LOGIN_NAME_MAX</tt>, <tt>OPEN_MAX</tt>, <tt>PAGESIZE</tt>, <tt>RE_DUP_MAX</tt>, <tt>STREAM_MAX</tt>, <tt>SYMLOOP_MAX</tt>, <tt>TTY_NAME_MAX</tt>, and <tt>TZNAME_MAX</tt></P></div></li><li><div style="font-weight:normal"><p class="docList">Pathname variable values, possibly indeterminate: <tt>FILESIZEBITS</tt>, <tt>LINK_MAX</tt>, <tt>MAX_CANON</tt>, <tt>MAX_INPUT</tt>, <tt>NAME_MAX</tt>, <tt>PATH_MAX</tt>, <tt>PIPE_BUF</tt>, and <tt>SYMLINK_MAX</tt></p></div></li></ol></div>
<a name="ch02fig08"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><h5 class="docTableTitle">Figure 2.8. POSIX.1 invariant minimum values from <tt>&lt;limits.h&gt;</tt></h5></caption><colgroup><col width="175"><col width="260"><col width="65"></colgroup><thead><tr><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle"><p class="docText"><span class="docEmphRoman">Name</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Description: minimum acceptable value for</span></p></th><th class="bottomBorder thead" scope="col" align="center" valign="middle"><p class="docText"><span class="docEmphRoman">Value</span></p></th></tr></thead><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_ARG_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">length of arguments to <tt>exec</tt> functions</P></TD><td class="docTableCell" align="right" valign="top"><p class="docText">4,096</P></TD></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_CHILD_MAX</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">number of child processes per real user ID</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">25</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_HOST_NAME_MAX</tt></p></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum length of a host name as returned by <tt>gethostname</tt></p></td><td class="docTableCell" align="right" valign="top"><p class="docText">255</p></TD></tr><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_LINK_MAX</tt></P></td><td class="rightBorder" align="left" valign="top"><p class="docText">number of links to a file</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">8</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_LOGIN_NAME_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum length of a login name</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">9</p></TD></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_MAX_CANON</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">number of bytes on a terminal's canonical input queue</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">255</p></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_MAX_INPUT</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">space available on a terminal's input queue</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">255</P></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_NAME_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">number of bytes in a filename, not including the terminating null</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">14</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_NGROUPS_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">number of simultaneous supplementary group IDs per process</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">8</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_OPEN_MAX</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">number of open files per process</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">20</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_PATH_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">number of bytes in a pathname, including the terminating null</P></TD><td class="docTableCell" align="right" valign="top"><p class="docText">256</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_PIPE_BUF</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">number of bytes that can be written atomically to a pipe</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">512</P></td></TR><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_RE_DUP_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">number of repeated occurrences of a basic regular expression permitted by the <tt>regexec</tt> and <tt>regcomp</tt> functions when using the interval notation <tt>\{m,n\}</tt></p></td><td class="docTableCell" align="right" valign="top"><p class="docText">255</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_SSIZE_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">value that can be stored in <tt>ssize_t</tt> object</p></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">32,767</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_STREAM_MAX</tt></P></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">number of standard I/O streams a process can have open at once</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">8</P></TD></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_SYMLINK_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">number of bytes in a symbolic link</P></td><td class="docTableCell" align="right" valign="top"><p class="docText">255</p></td></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_SYMLOOP_MAX</tt></p></TD><td class="rightBorder" align="left" valign="top"><p class="docText">number of symbolic links that can be traversed during pathname resolution</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">8</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_TTY_NAME_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">length of a terminal device name, including the terminating null</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">9</p></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_POSIX_TZNAME_MAX</tt></P></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">number of bytes for the name of a time zone</p></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">6</P></td></TR></table></p><BR>
<p class="docText">Of these 44 limits and constants, some may be defined in <tt>&lt;limits.h&gt;</tt>, and others may or may not be defined, depending on certain conditions. We describe the limits and constants that may or may not be defined in <a class="docLink" href="#ch02lev2sec15">Section 2.5.4</a>, when we describe the <tt>sysconf</tt>, <tt>pathconf</tt>, and <tt>fpathconf</tt> functions. The 19 invariant minimum values are shown in <a class="docLink" href="#ch02fig08">Figure 2.8</a>.</P>
<p class="docText">These values are invariant; they do not change from one system to another. They specify the most restrictive values for these features. A conforming POSIX.1 implementation must provide values that are at least this large. This is why they are called minimums, although their names all contain <tt>MAX</tt>. Also, to ensure portability, a <a name="idd1e11747"></a><a name="idd1e11752"></a><a name="idd1e11757"></a><a name="idd1e11762"></a><a name="idd1e11767"></a><a name="idd1e11770"></a><a name="idd1e11775"></a><a name="idd1e11780"></a><a name="idd1e11785"></a><a name="idd1e11790"></a>strictly-conforming application must not require a larger value. We describe what each of these constants refers to as we proceed through the text.</P>
<blockquote>
<p class="docText">A strictly-conforming POSIX application is different from an application that is merely POSIX conforming. A POSIX-conforming application uses only interfaces defined in IEEE Standard 1003.1-2001. A strictly-conforming application is a POSIX-conforming application that does not rely on any undefined behavior, does not use any obsolescent interfaces, and does not require values of constants larger than the minimums shown in <a class="docLink" href="#ch02fig08">Figure 2.8</a>.</p>
</blockquote>
<p class="docText">Unfortunately, some of these invariant minimum values are too small to be of practical use. For example, most UNIX systems today provide far more than 20 open files per process. Also, the minimum limit of 255 for <tt>_POSIX_PATH_MAX</tt> is too small. Pathnames can exceed this limit. This means that we can't use the two constants <tt>_POSIX_OPEN_MAX</tt> and <tt>_POSIX_PATH_MAX</tt> as array sizes at compile time.</P>
<p class="docText">Each of the 19 invariant minimum values in <a class="docLink" href="#ch02fig08">Figure 2.8</a> has an associated implementation value whose name is formed by removing the <tt>_POSIX_</tt> prefix from the name in <a class="docLink" href="#ch02fig08">Figure 2.8</a>. The names without the leading <tt>_POSIX_</tt> were intended to be the actual values that a given implementation supports. (These 19 implementation values are items 25 from our list earlier in this section: the invariant value, the runtime increasable value, the runtime invariant values, and the pathname variable values.) The problem is that not all of the 19 implementation values are guaranteed to be defined in the <tt>&lt;limits.h&gt;</tt> header.</P>
<p class="docText">For example, a particular value may not be included in the header if its actual value for a given process depends on the amount of memory on the system. If the values are not defined in the header, we can't use them as array bounds at compile time. So, POSIX.1 decided to provide three runtime functions for us to call<tt>sysconf</tt>, <tt>pathconf</tt>, and <tt>fpathconf</tt>to determine the actual implementation value at runtime. There is still a problem, however, because some of the values are defined by POSIX.1 as being possibly &quot;indeterminate&quot; (logically infinite). This means that the value has no practical upper bound. On Linux, for example, the number of <tt>iovec</tt> structures you can use with <tt>readv</tt> or <tt>writev</tt> is limited only by the amount of memory on the system. Thus, <tt>IOV_MAX</tt> is considered indeterminate on Linux. We'll return to this problem of indeterminate runtime limits in <a class="docLink" href="#ch02lev2sec16">Section 2.5.5</a>.</p>

<a name="ch02lev2sec14"></a>
<H4 class="docSection2Title">2.5.3. XSI Limits</H4>
<p class="docText">The XSI also defines constants that deal with implementation limits. They include:</p>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">Invariant minimum values: the ten constants in <a class="docLink" href="#ch02fig09">Figure 2.9</a></p></div></li><LI><div style="font-weight:normal"><p class="docList">Numerical limits: <tt>LONG_BIT</tt> and <tt>WORD_BIT</tt></p></div></LI><li><div style="font-weight:normal"><p class="docList">Runtime invariant values, possibly indeterminate: <tt>ATEXIT_MAX</tt>, <tt>IOV_MAX</tt>, and <tt>PAGE_SIZE</tt></P></div></li></ol></div>
<a name="ch02fig09"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><h5 class="docTableTitle">Figure 2.9. XSI invariant minimum values from <tt>&lt;limits.h&gt;</tt></h5></caption><colgroup><col width="125"><col width="175"><col width="100"><col width="100"></colgroup><thead><tr><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle"><p class="docText"><span class="docEmphRoman">Name</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle"><p class="docText"><span class="docEmphRoman">Description</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Minimum acceptable value</span></p></th><th class="bottomBorder thead" scope="col" align="center" valign="middle"><p class="docText"><span class="docEmphRoman">Typical value</span></p></th></tr></thead><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NL_ARGMAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum value of digit in calls to <tt>printf</tt> and <tt>scanf</tt></p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">9</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">9</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>NL_LANGMAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes in <tt>LANG</tt> environment variable</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">14</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">14</P></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NL_MSGMAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum message number</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">32,767</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NL_NMAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes in <span class="docEmphasis">N</span>-to-1 mapping characters</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">(none specified)</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">1</p></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NL_SETMAX</tt></P></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum set number</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">255</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">255</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NL_TEXTMAX</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes in a message string</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText"><tt>_POSIX2_LINE_MAX</tt></p></td><td class="docTableCell" align="right" valign="top"><p class="docText">2,048</P></td></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>NZERO</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">default process priority</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">20</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">20</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_XOPEN_IOV_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of <tt>iovec</tt> structures that can be used with <tt>readv</tt> or <tt>writev</tt></P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">16</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">16</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_XOPEN_NAME_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">number of bytes in a filename</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">255</P></TD><td class="docTableCell" align="right" valign="top"><p class="docText">255</P></TD></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>_XOPEN_PATH_MAX</tt></p></TD><td class="rightBorder" align="left" valign="top"><p class="docText">number of bytes in a pathname</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">1,024</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">1,024</p></td></tr></table></p><br>
<p class="docText">The invariant minimum values are listed in <a class="docLink" href="#ch02fig09">Figure 2.9</a>. Many of these values deal with message catalogs. The last two illustrate the situation in which the POSIX.1 minimums were too smallpresumably to allow for embedded POSIX.1 implementationsso the <a name="idd1e12183"></a><a name="idd1e12189"></a><a name="idd1e12196"></a><a name="idd1e12201"></a><a name="idd1e12206"></a><a name="idd1e12211"></a><a name="idd1e12216"></a><a name="idd1e12221"></a><a name="idd1e12226"></a><a name="idd1e12231"></a><a name="idd1e12236"></a><a name="idd1e12243"></a><a name="idd1e12248"></a><a name="idd1e12253"></a><a name="idd1e12258"></a><a name="idd1e12261"></a><a name="idd1e12268"></a><a name="idd1e12273"></a><a name="idd1e12278"></a>Single UNIX Specification added symbols with larger minimum values for XSI-conforming systems.</p>

<a name="ch02lev2sec15"></a>
<h4 class="docSection2Title">2.5.4. <tt>sysconf</tt>, <tt>pathconf</tt>, and <tt>fpathconf</tt> Functions</h4>
<p class="docText">We've listed various minimum values that an implementation must support, but how do we find out the limits that a particular system actually supports? As we mentioned earlier, some of these limits might be available at compile time; others must be determined at runtime. We've also mentioned that some don't change in a given system, whereas others can change because they are associated with a file or directory. The runtime limits are obtained by calling one of the following three functions.</p>
<a name="inta175"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="none" cellpadding="5"><colgroup><col width="500"></colgroup><thead></thead><tr><td class="docTableCell" align="left" valign="top"><p class="docText">
<pre>
#include &lt;unistd.h&gt;

long sysconf(int <span class="docEmphItalicAlt">name</span>);

long pathconf(const char *<span class="docEmphItalicAlt">pathname</span>, int <span class="docEmphItalicAlt">name</span>);

long fpathconf(int <span class="docEmphItalicAlt">filedes</span>, int <span class="docEmphItalicAlt">name</span>);
</pre><br>

</p></TD></TR><tr><TD class="docTableCell" align="right" valign="top"><p class="docText">All three return: corresponding value if OK, 1 on error (see later)</P></TD></tr></table></P><BR>
<p class="docText">The difference between the last two functions is that one takes a pathname as its argument and the other takes a file descriptor argument.</P>
<p class="docText"><a class="docLink" href="#ch02fig10">Figure 2.10</a> lists the <span class="docEmphasis">name</span> arguments that <tt>sysconf</tt> uses to identify system limits. Constants beginning with <tt>_SC_</tt> are used as arguments to <tt>sysconf</tt> to identify the runtime limit. <a class="docLink" href="#ch02fig11">Figure 2.11</a> lists the <span class="docEmphasis">name</span> arguments that are used by <tt>pathconf</tt> and <tt>fpathconf</tt> to identify system limits. Constants beginning with <tt>_PC_</tt> are used as arguments to <tt>pathconf</tt> and <tt>fpathconf</tt> to identify the runtime limit.</p>
<a name="ch02fig10"></a><P><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><h5 class="docTableTitle">Figure 2.10. Limits and <span class="docEmphasis">name</span> arguments to <tt>sysconf</tt></H5></caption><colgroup><col width="125"><col width="250"><col width="125"></colgroup><thead><TR><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Name of limit</span></P></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Description</span></p></th><th class="bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman"><span class="docEmphasis">name</span> argument</span></P></th></TR></thead><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>ARG_MAX</tt></P></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum length, in bytes, of arguments to the <tt>exec</tt> functions</p></td><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_ARG_MAX</tt>
</p></TD></tr><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>ATEXIT_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of functions that can be registered with the <tt>atexit</tt> function</p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_ATEXIT_MAX</tt>
</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>CHILD_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of processes per real user ID</p></td><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_CHILD_MAX</tt>
</P></td></TR><TR><TD class="rightBorder" align="left" valign="top"><p class="docText">clock ticks/second</p></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">number of clock ticks per second</P></td><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_CLK_TCK</tt>
</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>COLL_WEIGHTS_MAX</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of weights that can be assigned to an entry of the <tt>LC_COLLATE</tt> order keyword in the locale definition file</P></TD><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_COLL_WEIGHTS_MAX</tt>
</p></td></tr><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>HOST_NAME_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum length of a host name as returned by <tt>gethostname</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_HOST_NAME_MAX</tt>
</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>IOV_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of <tt>iovec</tt> structures that can be used with <tt>readv</tt> or <tt>writev</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_IOV_MAX</tt>
</p></td></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>LINE_MAX</tt></p></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum length of a utility's input line</P></td><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_LINE_MAX</tt>
</P></TD></tr><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>LOGIN_NAME_MAX</tt></P></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum length of a login name</p></TD><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_LOGIN_NAME_MAX</tt>
</p></TD></TR><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NGROUPS_MAX</tt></p></td><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum number of simultaneous supplementary process group IDs per process</p></TD><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_NGROUPS_MAX</tt>
</P></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>OPEN_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of open files per process</p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_OPEN_MAX</tt>
</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>PAGESIZE</tt></p></td><TD class="rightBorder" align="left" valign="top"><p class="docText">system memory page size, in bytes</P></td><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_PAGESIZE</tt>
</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>PAGE_SIZE</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">system memory page size, in bytes</p></TD><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_PAGE_SIZE</tt>
</P></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>RE_DUP_MAX</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">number of repeated occurrences of a basic regular expression permitted by the <tt>regexec</tt> and <tt>regcomp</tt> functions when using the interval notation <tt>\{m,n\}</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_RE_DUP_MAX</tt>
</P></td></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>STREAM_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of standard I/O streams per process at any given time; if defined, it must have the same value as <tt>FOPEN_MAX</tt></p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_STREAM_MAX</tt>
</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>SYMLOOP_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">number of symbolic links that can be traversed during pathname resolution</p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_SYMLOOP_MAX</tt>
</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>TTY_NAME_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">length of a terminal device name, including the terminating null</P></TD><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_TTY_NAME_MAX</tt>
</P></td></TR><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>TZNAME_MAX</tt></p></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes for the name of a time zone</p></TD><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_SC_TZNAME_MAX</tt>
</p></td></tr></table></p><BR>
<a name="ch02fig11"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><H5 class="docTableTitle">Figure 2.11. Limits and <span class="docEmphasis">name</span> arguments to <tt>pathconf</tt> and <tt>fpathconf</tt></h5></caption><colgroup><col width="125"><col width="250"><col width="125"></colgroup><thead><TR><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Name of limit</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">Description</span></p></th><th class="bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman"><span class="docEmphasis">name</span> argument</span></p></th></tr></thead><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>FILESIZEBITS</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">minimum number of bits needed to represent, as a signed integer value, the maximum size of a regular file allowed in the specified directory</p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_FILESIZEBITS</tt>
</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>LINK_MAX</tt></P></TD><td class="rightBorder" align="left" valign="top"><p class="docText">maximum value of a file's link count</P></TD><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_LINK_MAX</tt>
</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>MAX_CANON</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes on a terminal's canonical input queue</P></TD><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_MAX_CANON</tt>
</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>MAX_INPUT</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">number of bytes for which space is available on terminal's input queue</p></TD><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_MAX_INPUT</tt>
</P></td></TR><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NAME_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes in a filename (does not include a null at end)</p></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_NAME_MAX</tt></p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>PATH_MAX</tt></p></td><td class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes in a relative pathname, including the terminating null</p></TD><TD class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_PATH_MAX</tt>
</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>PIPE_BUF</tt></P></TD><TD class="rightBorder" align="left" valign="top"><p class="docText">maximum number of bytes that can be written atomically to a pipe</p></TD><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_PIPE_BUF</tt>
</P></TD></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>SYMLINK_MAX</tt></P></td><TD class="rightBorder" align="left" valign="top"><p class="docText">number of bytes in a symbolic link</P></td><td class="docTableCell" align="left" valign="top"><p class="docText"><tt>_PC_SYMLINK_MAX</tt>
</p></td></TR></table></p><BR>
<p class="docText"><a name="idd1e12977"></a><a name="idd1e12982"></a><a name="idd1e12987"></a><a name="idd1e12992"></a><a name="idd1e12997"></a><a name="idd1e13000"></a><a name="idd1e13005"></a><a name="idd1e13010"></a><a name="idd1e13016"></a><a name="idd1e13021"></a><a name="idd1e13026"></a><a name="idd1e13031"></a><a name="idd1e13036"></a><a name="idd1e13041"></a><a name="idd1e13046"></a><a name="idd1e13051"></a><a name="idd1e13056"></a><a name="idd1e13059"></a><a name="idd1e13064"></a><a name="idd1e13069"></a><a name="idd1e13074"></a><a name="idd1e13079"></a><a name="idd1e13084"></a><a name="idd1e13089"></a><a name="idd1e13094"></a><a name="idd1e13099"></a><a name="idd1e13104"></a><a name="idd1e13109"></a><a name="idd1e13114"></a><a name="idd1e13119"></a><a name="idd1e13124"></a><a name="idd1e13129"></a><a name="idd1e13134"></a><a name="idd1e13139"></a><a name="idd1e13144"></a><a name="idd1e13149"></a><a name="idd1e13154"></a><a name="idd1e13159"></a><a name="idd1e13164"></a><a name="idd1e13169"></a><a name="idd1e13174"></a><a name="idd1e13179"></a><a name="idd1e13184"></a><a name="idd1e13189"></a><a name="idd1e13194"></a><a name="idd1e13199"></a><a name="idd1e13202"></a><a name="idd1e13207"></a><a name="idd1e13212"></a><a name="idd1e13217"></a>We need to look in more detail at the different return values from these three functions.</p>
<div style="font-weight:bold"><ol class="docList" type="1"><LI><div style="font-weight:normal"><p class="docList">All three functions return 1 and set <tt>errno</tt> to <tt>EINVAL</tt> if the <span class="docEmphasis">name</span> isn't one of the appropriate constants. The third column in <a class="docLink" href="#ch02fig10">Figures 2.10</a> and <a class="docLink" href="#ch02fig11">2.11</a> lists the limit constants we'll deal with throughout the rest of this book.</p></div></li><li><div style="font-weight:normal"><p class="docList">Some <span class="docEmphasis">name</span>s can return either the value of the variable (a return value <img src=images/ent/U2265.GIF border=0> 0) or an indication that the value is indeterminate. An indeterminate value is indicated by returning 1 and not changing the value of <tt>errno</tt>.</p></div></li><li><div style="font-weight:normal"><p class="docList">The value returned for <tt>_SC_CLK_TCK</tt> is the number of clock ticks per second, for use with the return values from the <tt>times</tt> function (<a class="docLink" href="ch08lev1sec16.html#ch08lev1sec16">Section 8.16</a>).</p></div></li></ol></div>
<p class="docText"><a name="idd1e13276"></a><a name="idd1e13279"></a><a name="idd1e13282"></a><a name="idd1e13287"></a><a name="idd1e13292"></a><a name="idd1e13295"></a><a name="idd1e13300"></a><a name="idd1e13305"></a><a name="idd1e13310"></a><a name="idd1e13315"></a><a name="idd1e13320"></a><a name="idd1e13325"></a><a name="idd1e13330"></a><a name="idd1e13335"></a><a name="idd1e13340"></a><a name="idd1e13345"></a><a name="idd1e13350"></a><a name="idd1e13355"></a><a name="idd1e13360"></a><a name="idd1e13365"></a><a name="idd1e13370"></a><a name="idd1e13375"></a><a name="idd1e13378"></a><a name="idd1e13383"></a>There are some restrictions for the <span class="docEmphasis">pathname</span> argument to <tt>pathconf</tt> and the <span class="docEmphasis">filedes</span> argument to <tt>fpathconf</tt>. If any of these restrictions isn't met, the results are undefined.</p>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">The referenced file for <tt>_PC_MAX_CANON</tt> and <tt>_PC_MAX_INPUT</tt> must be a terminal file.</p></div></li><li><div style="font-weight:normal"><p class="docList">The referenced file for <tt>_PC_LINK_MAX</tt> can be either a file or a directory. If the referenced file is a directory, the return value applies to the directory itself, not to the filename entries within the directory.</p></div></li><li><div style="font-weight:normal"><p class="docList">The referenced file for <tt>_PC_FILESIZEBITS</tt> and <tt>_PC_NAME_MAX</tt> must be a directory. The return value applies to filenames within the directory.</p></div></LI><LI><div style="font-weight:normal"><p class="docList">The referenced file for <tt>_PC_PATH_MAX</tt> must be a directory. The value returned is the maximum length of a relative pathname when the specified directory is the working directory. (Unfortunately, this isn't the real maximum length of an absolute pathname, which is what we want to know. We'll return to this problem in <a class="docLink" href="#ch02lev2sec16">Section 2.5.5</a>.)</p></div></LI><LI><div style="font-weight:normal"><p class="docList">The referenced file for <tt>_PC_PIPE_BUF</tt> must be a pipe, FIFO, or directory. In the first two cases (pipe or FIFO) the return value is the limit for the referenced pipe or FIFO. For the other case (a directory) the return value is the limit for any FIFO created in that directory.</P></div></li><LI><div style="font-weight:normal"><p class="docList">The referenced file for <tt>_PC_SYMLINK_MAX</tt> must be a directory. The value returned is the maximum length of the string that a symbolic link in that directory can contain.</P></div></LI></ol></div>
<a name="ch02ex01"></a>
<h5 class="docExampleTitle">Example</H5>
<p class="docText"><a name="idd1e13468"></a>The <tt>awk</tt>(1) program shown in <a class="docLink" href="#ch02fig12">Figure 2.12</a> builds a C program that prints the value of each <tt>pathconf</tt> and <tt>sysconf</tt> symbol.</p>
<p class="docText">The <tt>awk</tt> program reads two input files<tt>pathconf.sym</tt> and <tt>sysconf.sym</tt>that contain lists of the limit name and symbol, separated by tabs. All symbols are not defined on every platform, so the <tt>awk</tt> program surrounds each call to <tt>pathconf</tt> and <tt>sysconf</tt> with the necessary <tt>#ifdef</tt> statements.</P>
<p class="docText">For example, the <tt>awk</tt> program transforms a line in the input file that looks like</P>

<pre>
   NAME_MAX      _PC_NAME_MAX
</pre><BR>

<p class="docText">into the following C code:</p>

<pre>
#ifdef NAME_MAX
     printf("NAME_MAX is defined to be %d\n", NAME_MAX+0);
#else
     printf("no symbol for NAME_MAX\n");
#endif
#ifdef _PC_NAME_MAX
     pr_pathconf("NAME_MAX =", argv[1], _PC_NAME_MAX);
#else
     printf("no symbol for _PC_NAME_MAX\n");
#endif
</pre><BR>

<p class="docText"><a name="idd1e13531"></a><a name="idd1e13536"></a><a name="idd1e13541"></a><a name="idd1e13546"></a>The program in <a class="docLink" href="#ch02fig13">Figure 2.13</a>, generated by the <tt>awk</tt> program, prints all these limits, handling the case in which a limit is not defined.</P>
<p class="docText"><a name="idd1e13562"></a><a class="docLink" href="#ch02fig14">Figure 2.14</a> summarizes results from <a class="docLink" href="#ch02fig13">Figure 2.13</a> for the four systems we discuss in this book. The entry &quot;no symbol&quot; means that the system doesn't provide a corresponding <tt>_SC</tt> or <tt>_PC</tt> symbol to query the value of the constant. Thus, the limit is undefined in this case. In contrast, the entry &quot;unsupported&quot; means that the symbol is defined by the system but unrecognized by the <tt>sysconf</tt> or <tt>pathconf</tt> functions. The entry &quot;no limit&quot; means that the system defines no limit for the constant, but this doesn't mean that the limit is infinite.</p>
<p class="docText"><a name="idd1e13590"></a><a name="idd1e13595"></a><a name="idd1e13600"></a><a name="idd1e13605"></a><a name="idd1e13610"></a><a name="idd1e13613"></a><a name="idd1e13618"></a><a name="idd1e13623"></a><a name="idd1e13628"></a><a name="idd1e13633"></a><a name="idd1e13636"></a><a name="idd1e13641"></a><a name="idd1e13646"></a><a name="idd1e13651"></a><a name="idd1e13656"></a><a name="idd1e13661"></a><a name="idd1e13666"></a><a name="idd1e13669"></a><a name="idd1e13674"></a><a name="idd1e13677"></a><a name="idd1e13682"></a><a name="idd1e13687"></a><a name="idd1e13692"></a><a name="idd1e13697"></a><a name="idd1e13702"></a><a name="idd1e13707"></a><a name="idd1e13712"></a><a name="idd1e13717"></a><a name="idd1e13720"></a><a name="idd1e13725"></a><a name="idd1e13730"></a><a name="idd1e13733"></a><a name="idd1e13738"></a><a name="idd1e13741"></a><a name="idd1e13746"></a><a name="idd1e13751"></a><a name="idd1e13754"></a><a name="idd1e13759"></a><a name="idd1e13764"></a>We'll see in <a class="docLink" href="ch04lev1sec14.html#ch04lev1sec14">Section 4.14</a> that UFS is the SVR4 implementation of the Berkeley fast file system. PCFS is the MS-DOS FAT file system implementation for Solaris.</P>

<a name="ch02fig12"></a>
<H5 class="docExampleTitle">Figure 2.12. Build C program to print all supported configuration limits</h5>

<pre>
BEGIN   {
    printf("#include \"apue.h\"\n")
    printf("#include &lt;errno.h&gt;\n")
    printf("#include &lt;limits.h&gt;\n")
    printf("\n")
    printf("static void pr_sysconf(char *, int);\n")
    printf("static void pr_pathconf(char *, char *, int);\n")
    printf("\n")
    printf("int\n")
    printf("main(int argc, char *argv[])\n")
    printf("{\n")
    printf("\tif (argc != 2)\n")
    printf("\t\terr_quit(\"usage: a.out &lt;dirname&gt;\");\n\n")
    FS="\t+"
    while (getline &lt;"sysconf.sym" &gt; 0) {
        printf("#ifdef %s\n", $1)
        printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
        printf("#else\n")
        printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
        printf("#endif\n")
        printf("#ifdef %s\n", $2)
        printf("\tpr_sysconf(\"%s =\", %s);\n", $1, $2)
        printf("#else\n")
        printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
        printf("#endif\n")
    }
    close("sysconf.sym")
    while (getline &lt;"pathconf.sym" &gt; 0) {
        printf("#ifdef %s\n", $1)
        printf("\tprintf(\"%s defined to be %%d\\n\", %s+0);\n", $1, $1)
        printf("#else\n")
        printf("\tprintf(\"no symbol for %s\\n\");\n", $1)
        printf("#endif\n")
        printf("#ifdef %s\n", $2)
        printf("\tpr_pathconf(\"%s =\", argv[1], %s);\n", $1, $2)
        printf("#else\n")
        printf("\tprintf(\"no symbol for %s\\n\");\n", $2)
        printf("#endif\n")
    }
    close("pathconf.sym")
    exit
}
END {
    printf("\texit(0);\n")
    printf("}\n\n")
    printf("static void\n")
    printf("pr_sysconf(char *mesg, int name)\n")
    printf("{\n")
    printf("\tlong val;\n\n")
    printf("\tfputs(mesg, stdout);\n")
    printf("\terrno = 0;\n")
    printf("\tif ((val = sysconf(name)) &lt; 0) {\n")
    printf("\t\tif (errno != 0) {\n")
    printf("\t\t\tif (errno == EINVAL)\n")
    printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
    printf("\t\t\telse\n")
    printf("\t\t\t\terr_sys(\"sysconf error\");\n")
    printf("\t\t} else {\n")
    printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
    printf("\t\t}\n")
    printf("\t} else {\n")
    printf("\t\tprintf(\" %%ld\\n\", val);\n")
    printf("\t}\n")
    printf("}\n\n")
    printf("static void\n")
    printf("pr_pathconf(char *mesg, char *path, int name)\n")
    printf("{\n")
    printf("\tlong val;\n")
    printf("\n")
    printf("\tfputs(mesg, stdout);\n")
    printf("\terrno = 0;\n")
    printf("\tif ((val = pathconf(path, name)) &lt; 0) {\n")
    printf("\t\tif (errno != 0) {\n")
    printf("\t\t\tif (errno == EINVAL)\n")
    printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")
    printf("\t\t\telse\n")
    printf("\t\t\t\terr_sys(\"pathconf error, path = %%s\", path);\n")
    printf("\t\t} else {\n")
    printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")
    printf("\t\t}\n")
    printf("\t} else {\n")
    printf("\t\tprintf(\" %%ld\\n\", val);\n")
    printf("\t}\n")
    printf("}\n")
}
</pre><br>


<a name="ch02fig13"></a>
<h5 class="docExampleTitle">Figure 2.13. Print all possible <tt>sysconf</tt> and <tt>pathconf</tt> values</h5>

<pre>
#include "apue.h"
#include &lt;errno.h&gt;
#include &lt;limits.h&gt;

static void pr_sysconf(char *, int);
static void pr_pathconf(char *, char *, int);

int
main(int argc, char *argv[])
{
    if (argc != 2)
        err_quit("usage: a.out &lt;dirname&gt;");

#ifdef ARG_MAX
    printf("ARG_MAX defined to be %d\n", ARG_MAX+0);
#else
    printf("no symbol for ARG_MAX\n");
#endif
#ifdef _SC_ARG_MAX
    pr_sysconf("ARG_MAX =", _SC_ARG_MAX);
#else
    printf("no symbol for _SC_ARG_MAX\n");
#endif

/* similar processing for all the rest of the sysconf symbols... */

#ifdef MAX_CANON
    printf("MAX_CANON defined to be %d\n", MAX_CANON+0);
#else
    printf("no symbol for MAX_CANON\n");
#endif
#ifdef _PC_MAX_CANON
    pr_pathconf("MAX_CANON =", argv[1], _PC_MAX_CANON);
#else
    printf("no symbol for _PC_MAX_CANON\n");
#endif

/* similar processing for all the rest of the pathconf symbols... */

   exit(0);
}
static void
pr_sysconf(char *mesg, int name)
{
    long    val;

    fputs(mesg, stdout);
    errno = 0;
    if ((val = sysconf(name)) &lt; 0) {
        if (errno != 0) {
            if (errno == EINVAL)
                fputs(" (not supported)\n", stdout);
            else
                err_sys("sysconf error");
        } else {
            fputs(" (no limit)\n", stdout);
        }
    } else {
        printf(" %ld\n", val);
    }
}

static void
pr_pathconf(char *mesg, char *path, int name)
{
    long    val;

    fputs(mesg, stdout);
    errno = 0;
    if ((val = pathconf(path, name)) &lt; 0) {
        if (errno != 0) {
            if (errno == EINVAL)
                fputs(" (not supported)\n", stdout);
            else
                err_sys("pathconf error, path = %s", path);
        } else {
            fputs(" (no limit)\n", stdout);
        }
    } else {
        printf(" %ld\n", val);
    }
}
</pre><BR>


<a name="ch02fig14"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><H5 class="docTableTitle">Figure 2.14. Examples of configuration limits</h5></caption><colgroup><col width="150"><col width="75"><col width="75"><col width="75"><col width="75"><col width="75"></colgroup><thead><TR><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle" rowspan="2"><p class="docText"><span class="docEmphRoman">Limit</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle" rowspan="2"><p class="docText"><span class="docEmphRoman">FreeBSD 5.2.1</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle" rowspan="2"><p class="docText"><span class="docEmphRoman">Linux 2.4.22</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle" rowspan="2"><p class="docText"><span class="docEmphRoman">Mac OS X 10.3</span></p></th><th class="bottomBorder thead" scope="col" align="center" valign="bottom" colspan="2"><p class="docText"><span class="docEmphRoman">Solaris 9</span></p></th></tr><tr><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">UFS file system</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="bottom"><p class="docText"><span class="docEmphRoman">PCFS file system</span></p></th></tr></thead><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>ARG_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">65,536</p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">131,072</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">262,144</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">1,048,320</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">1,048,320</p></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>ATEXIT_MAX</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">32</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">2,147,483,647</P></td><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">no limit</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">no limit</P></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>CHARCLASS_NAME_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">2,048</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">14</p></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">14</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>CHILD_MAX</tt></P></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">867</p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">999</P></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">100</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">7,877</p></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">7,877</p></td></tr><tr><TD class="rightBorder" align="left" valign="top"><p class="docText">clock ticks/second</p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">128</P></td><td class="rightBorder" align="right" valign="top"><p class="docText">100</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">100</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">100</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">100</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>COLL_WEIGHTS_MAX</tt></p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">0</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">255</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">2</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">10</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">10</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>FILESIZEBITS</tt></p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">unsupported</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">64</p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">41</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">unsupported</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>HOST_NAME_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">unsupported</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">no symbol</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>IOV_MAX</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">1,024</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no limit</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">16</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">16</P></td></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>LINE_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">2,048</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">2,048</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">2,048</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">2,048</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">2,048</p></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>LINK_MAX</tt></P></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">32,000</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">32,767</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">1</P></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>LOGIN_NAME_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">17</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">256</p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">9</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">9</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>MAX_CANON</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">255</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">255</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">256</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">256</p></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>MAX_INPUT</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">255</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">255</P></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">512</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">512</P></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NAME_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">765</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">8</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>NGROUPS_MAX</tt></P></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">16</p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">32</P></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">16</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">16</p></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">16</p></td></tr><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>OPEN_MAX</tt></p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">1,735</P></td><td class="rightBorder" align="right" valign="top"><p class="docText">1,024</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">256</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">256</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">256</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>PAGESIZE</tt></p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">4,096</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">4,096</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">4,096</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">8,192</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">8,192</P></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>PAGE_SIZE</tt></p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">4,096</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">4,096</p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">8,192</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">8,192</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>PATH_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">1,024</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">4,096</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">1,024</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">1,024</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">1,024</p></TD></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>PIPE_BUF</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">512</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">4,096</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">512</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">5,120</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">5,120</P></td></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>RE_DUP_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">32,767</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">255</p></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>STREAM_MAX</tt></P></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">1,735</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">16</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">20</p></TD><TD class="rightBorder" align="right" valign="top"><p class="docText">256</P></td><TD class="docTableCell" align="right" valign="top"><p class="docText">256</P></td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>SYMLINK_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">unsupported</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">no limit</p></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</p></td><td class="docTableCell" align="right" valign="top"><p class="docText">no symbol</p></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>SYMLOOP_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">32</p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">no limit</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">no symbol</P></TD><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</P></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">no symbol</p></TD></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>TTY_NAME_MAX</tt></P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">255</P></td><TD class="rightBorder" align="right" valign="top"><p class="docText">32</P></td><td class="rightBorder" align="right" valign="top"><p class="docText">no symbol</p></td><TD class="rightBorder" align="right" valign="top"><p class="docText">128</p></TD><td class="docTableCell" align="right" valign="top"><p class="docText">128</P></td></tr><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>TZNAME_MAX</tt></p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">6</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">255</p></td><td class="rightBorder" align="right" valign="top"><p class="docText">no limit</p></TD><TD class="docTableCell" align="right" valign="top"><p class="docText">no limit</p></TD></TR></table></P><br>

<a name="ch02lev2sec16"></a>
<H4 class="docSection2Title">2.5.5. Indeterminate Runtime Limits</H4>
<p class="docText">We mentioned that some of the limits can be indeterminate. The problem we encounter is that if these limits aren't defined in the <tt>&lt;limits.h&gt;</tt> header, we can't use them at compile time. But they might not be defined at runtime if their value is indeterminate! Let's look at two specific cases: allocating storage for a pathname and determining the number of file descriptors.</P>
<a name="ch02lev3sec1"></a>
<h5 class="docSection3Title">Pathnames</H5>
<p class="docText">Many programs need to allocate storage for a pathname. Typically, the storage has been allocated at compile time, and various magic numbersfew of which are the correct valuehave been used by different programs as the array size: 256, 512, 1024, or the <a name="idd1e14674"></a><a name="idd1e14677"></a><a name="idd1e14682"></a><a name="idd1e14687"></a><a name="idd1e14692"></a><a name="idd1e14697"></a><a name="idd1e14702"></a><a name="idd1e14707"></a><a name="idd1e14714"></a><a name="idd1e14719"></a><a name="idd1e14724"></a><a name="idd1e14729"></a><a name="idd1e14732"></a><a name="idd1e14737"></a><a name="idd1e14740"></a><a name="idd1e14745"></a>standard I/O constant <tt>BUFSIZ</tt>. The 4.3BSD constant <tt>MAXPATHLEN</tt> in the header <tt>&lt;sys/param.h&gt;</tt> is the correct value, but many 4.3BSD applications didn't use it.</p>
<p class="docText">POSIX.1 tries to help with the <tt>PATH_MAX</tt> value, but if this value is indeterminate, we're still out of luck. <a class="docLink" href="#ch02fig15">Figure 2.15</a> shows a function that we'll use throughout this text to allocate storage dynamically for a pathname.</P>
<a name="ch02fig15"></a>
<H5 class="docExampleTitle">Figure 2.15. Dynamically allocate space for a pathname</H5>

<pre>
#include "apue.h"
#include &lt;errno.h&gt;
#include &lt;limits.h&gt;

#ifdef  PATH_MAX
static int  pathmax = PATH_MAX;
#else
static int  pathmax = 0;
#endif

#define SUSV3   200112L

static long posix_version = 0;

/* If PATH_MAX is indeterminate, no guarantee this is adequate */
#define PATH_MAX_GUESS  1024

char *
path_alloc(int *sizep) /* also return allocated size, if nonnull */
{
    char    *ptr;
    int size;

    if (posix_version == 0)
        posix_version = sysconf(_SC_VERSION);

    if (pathmax == 0) {     /* first time through */
        errno = 0;
        if ((pathmax = pathconf("/", _PC_PATH_MAX)) &lt; 0) {
            if (errno == 0)
                pathmax = PATH_MAX_GUESS; /* it's indeterminate */
            else
                err_sys("pathconf error for _PC_PATH_MAX");
        } else {
            pathmax++;      /* add one since it's relative to root */
        }
    }
    if (posix_version &lt; SUSV3)
        size = pathmax + 1;
    else
        size = pathmax;

    if ((ptr = malloc(size)) == NULL)
        err_sys("malloc error for pathname");

    if (sizep != NULL)
        *sizep = size;
    return(ptr);
}
</pre><br>


<p class="docText">If the constant <tt>PATH_MAX</tt> is defined in <tt>&lt;limits.h&gt;</tt>, then we're all set. If it's not, we need to call <tt>pathconf</tt>. The value returned by <tt>pathconf</tt> is the maximum size of a relative pathname when the first argument is the working directory, so we specify the root as the first argument and add 1 to the result. If <tt>pathconf</tt> indicates that <tt>PATH_MAX</tt> is indeterminate, we have to punt and just guess a value.</P>
<p class="docText">Standards prior to SUSv3 were unclear as to whether or not <tt>PATH_MAX</tt> included a null byte at the end of the pathname. If the operating system implementation conforms to one of these prior versions, we need to add 1 to the amount of memory we allocate for a pathname, just to be on the safe side.</P>
<p class="docText">The correct way to handle the case of an indeterminate result depends on how the allocated space is being used. If we were allocating space for a call to <tt>getcwd</tt>, for exampleto return the absolute pathname of the current working directory; see <a class="docLink" href="ch04lev1sec22.html#ch04lev1sec22">Section 4.22</a>and if the allocated space is too small, an error is returned and <tt>errno</tt> is set to <tt>ERANGE</tt>. We could then increase the allocated space by calling <tt>realloc</tt> (see <a class="docLink" href="ch07lev1sec8.html#ch07lev1sec8">Section 7.8</a> and <a class="docLink" href="ch04lev1sec26.html#ch04qa1q16">Exercise 4.16</a>) and try again. We could keep doing this until the call to <tt>getcwd</tt> succeeded.</p>

<a name="ch02lev3sec2"></a>
<H5 class="docSection3Title">Maximum Number of Open Files</H5>
<p class="docText"><a name="idd1e14858"></a><a name="idd1e14863"></a><a name="idd1e14868"></a><a name="idd1e14873"></a><a name="idd1e14878"></a><a name="idd1e14883"></a>A common sequence of code in a daemon processa process that runs in the background, not connected to a terminalis one that closes all open files. Some programs have the following code sequence, assuming the constant <tt>NOFILE</tt> was defined in the <tt>&lt;sys/param.h&gt;</tt> header:</p>

<pre>
   #include  &lt;sys/param.h&gt;

   for (i = 0; i &lt; NOFILE; i++)
       close(i);
</pre><br>

<p class="docText">Other programs use the constant <tt>_NFILE</tt> that some versions of <tt>&lt;stdio.h&gt;</tt> provide as the upper limit. Some hard code the upper limit as 20.</p>
<p class="docText">We would hope to use the POSIX.1 value <tt>OPEN_MAX</tt> to determine this value portably, but if the value is indeterminate, we still have a problem. If we wrote the following and if <tt>OPEN_MAX</tt> was indeterminate, the loop would never execute, since <tt>sysconf</tt> would return -1:</p>

<pre>
   #include  &lt;unistd.h&gt;

   for (i = 0; i &lt; sysconf(_SC_OPEN_MAX); i++)
       close(i);
</pre><BR>

<p class="docText">Our best option in this case is just to close all descriptors up to some arbitrary limit, say 256. As with our pathname example, this is not guaranteed to work for all cases, but it's the best we can do. We show this technique in <a class="docLink" href="#ch02fig16">Figure 2.16</a>.</p>
<a name="ch02fig16"></a>
<H5 class="docExampleTitle">Figure 2.16. Determine the number of file descriptors</h5>

<pre>
#include "apue.h"
#include &lt;errno.h&gt;
#include &lt;limits.h&gt;

#ifdef  OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

/*
 * If OPEN_MAX is indeterminate, we're not
 * guaranteed that this is adequate.
 */
#define OPEN_MAX_GUESS 256

long
open_max(void)
{
    if (openmax == 0) {      /* first time through */
        errno = 0;
        if ((openmax = sysconf(_SC_OPEN_MAX)) &lt; 0) {
           if (errno == 0)
               openmax = OPEN_MAX_GUESS;    /* it's indeterminate */
           else
               err_sys("sysconf error for _SC_OPEN_MAX");
        }
    }

    return(openmax);
}
</pre><BR>


<p class="docText"><a name="idd1e14957"></a><a name="idd1e14962"></a><a name="idd1e14965"></a><a name="idd1e14970"></a><a name="idd1e14975"></a><a name="idd1e14980"></a><a name="idd1e14983"></a><a name="idd1e14988"></a><a name="idd1e14995"></a><a name="idd1e15000"></a>We might be tempted to call <tt>close</tt> until we get an error return, but the error return from <tt>close</tt> (<tt>EBADF</tt>) doesn't distinguish between an invalid descriptor and a descriptor that wasn't open. If we tried this technique and descriptor 9 was not open but descriptor 10 was, we would stop on 9 and never close 10. The <tt>dup</tt> function (<a class="docLink" href="ch03lev1sec12.html#ch03lev1sec12">Section 3.12</a>) does return a specific error when <tt>OPEN_MAX</tt> is exceeded, but duplicating a descriptor a couple of hundred times is an extreme way to determine this value.</p>
<p class="docText">Some implementations will return <tt>LONG_MAX</tt> for limits values that are effectively unlimited. Such is the case with the Linux limit for <tt>ATEXIT_MAX</tt> (see <a class="docLink" href="#ch02fig14">Figure 2.14</a>). This isn't a good idea, because it can cause programs to behave badly.</p>
<p class="docText">For example, we can use the <tt>ulimit</tt> command built into the Bourne-again shell to change the maximum number of files our processes can have open at one time. This generally requires special (superuser) privileges if the limit is to be effectively unlimited. But once set to infinite, <tt>sysconf</tt> will report <tt>LONG_MAX</tt> as the limit for <a name="idd1e15051"></a><a name="idd1e15054"></a><a name="idd1e15057"></a><a name="idd1e15062"></a><a name="idd1e15067"></a><a name="idd1e15070"></a><a name="idd1e15075"></a><a name="idd1e15078"></a><a name="idd1e15083"></a><a name="idd1e15088"></a><a name="idd1e15091"></a><a name="idd1e15096"></a><tt>OPEN_MAX</tt>. A program that relies on this value as the upper bound of file descriptors to close as shown in <a class="docLink" href="#ch02fig16">Figure 2.16</a> will waste a lot of time trying to close 2,147,483,647 file descriptors, most of which aren't even in use.</p>
<p class="docText">Systems that support the XSI extensions in the Single UNIX Specification will provide the <tt>getrlimit</tt>(2) function (<a class="docLink" href="ch07lev1sec11.html#ch07lev1sec11">Section 7.11</a>). It can be used to return the maximum number of descriptors that a process can have open. With it, we can detect that there is no configured upper bound to the number of open files our processes can open, so we can avoid this problem.</p>
<blockquote>
<p class="docText">The <tt>OPEN_MAX</tt> value is called runtime invariant by POSIX, meaning that its value should not change during the lifetime of a process. But on systems that support the XSI extensions, we can call the <tt>setrlimit</tt>(2) function (<a class="docLink" href="ch07lev1sec11.html#ch07lev1sec11">Section 7.11</a>) to change this value for a running process. (This value can also be changed from the C shell with the <tt>limit</tt> command, and from the Bourne, Bourne-again, and Korn shells with the <tt>ulimit</tt> command.) If our system supports this functionality, we could change the function in <a class="docLink" href="#ch02fig16">Figure 2.16</a> to call <tt>sysconf</tt> every time it is called, not only the first time.</p>
</blockquote>



<a href="17021535.html"><img src="images/pixel.gif" alt="" width="1" height="1" border="0"></a><ul></ul></td></tr></table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch02lev1sec4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch02lev1sec6.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html><br>
<table width="100%" cellspacing="0" cellpadding="0"
style="margin-top: 0pt; border-collapse: collapse;"> 
<tr> <td align="right" style="background-color=white; border-top: 1px solid gray;"> 
<a href="http://www.zipghost.com/" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">The CHM file was converted to HTM by Trial version of <b>ChmD<!--53-->ecompiler</b>.</a>
</TD>
</TR><tr>
<td align="right" style="background-color=white; "> 
<a href="http://www.etextwizard.com/download/cd/cdsetup.exe" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">Download <b>ChmDec<!--53-->ompiler</b> at: http://www.zipghost.com</a>
</TD></tr></table>
